<?php
require("dbFunctions.php");	//Required for connectDB

/**
 * RemoveResource takes a QR_Code (either Equipment or Room) and will set
 * the is_active field to false in the QR_Code table. We do not want to
 * actually delete entries or it could ruin the ResourceDesignation table,
 * so this is our solution.
 * If a resource is a container, dissolve all connections.
 * If a resource is contained in a package, just delete that row.
 * 
 * Author: Steve Gennaoui
 * Date: 11/12/2012
 */

$qr_code = $_REQUEST['qr_code'];

if( is_null($qr_code) )
	die("QR Code was not provided.");
if( !idIsValid($qr_code,"QR_Code") )
	die("QR_Code provided is invalid.");
elseif( isCurrentlyCheckedOut($qr_code) )
	die("Resource is currently checked out. Have the resource returned before attempting to remove it from the system.");

else
{
	//Deactivate the resource
	$dbh = connectDB();
	$query = "UPDATE QR_Code SET is_active=0 WHERE qr_code=:qr_code";
	$stmt = $dbh->prepare($query);
	$stmt->bindParam(':qr_code',$qr_code);
	$stmt->execute();

	//Delete all rows with the resource
	// If it was top level, this dissolves that package
	// If it was in a package, this removes the item from that package, without affecting
	//  the rest of the package.
	$query = "DELETE FROM Resource_Packages WHERE container_qr_code=:qr OR contained_qr_code=:qr";
	$stmt = $dbh->prepare($query);
	$stmt->bindParam(':qr',$qr_code);
	$stmt->execute();
}

?>
